package com.mlnx.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.bluwifi.manager.ConDeviceManage;
import com.mlnx.bluwifi.manager.SpoPolicy;
import com.mlnx.bluwifi.pojo.DataCache;
import com.mlnx.common.form.PageForm;
import com.mlnx.data.entity.BpInfo;
import com.mlnx.data.entity.SpoInfo;
import com.mlnx.device.dao.mysql.LanyaDeviceMapper;
import com.mlnx.device.entity.LanyaDevice;
import com.mlnx.device.enums.DeviceType;
import com.mlnx.device.pojo.form.ApDeviceForm;
import com.mlnx.device.service.LanyaDeviceService;
import com.mlnx.service.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by amanda.shan on 2019/11/18.
 */
@Service
public class LanyaDeviceServiceIml extends BaseService<LanyaDeviceMapper, LanyaDevice> implements LanyaDeviceService {

    private List<LanyaDevice> lanyaDevices;

    private long reacordLanyaDeviceTime;

    @Autowired
    @Override
    public void setBaseMapper(LanyaDeviceMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Override
    public List<LanyaDevice> list() {

        if (lanyaDevices == null || System.currentTimeMillis() - reacordLanyaDeviceTime > 10*1000){
            lanyaDevices = super.list();
            reacordLanyaDeviceTime = System.currentTimeMillis();
        }
        return lanyaDevices;
    }

    @Override
    public String getIpByBluMac(String mac) {

        List<LanyaDevice> lanyaDevices = list();

        if (lanyaDevices != null){
            mac = mac.toUpperCase();
            for (LanyaDevice lanyaDevice : lanyaDevices){
                if (lanyaDevice.getMac().equals(mac)){
                    return lanyaDevice.getIpAddress();
                }
            }
        }

        return null;
    }

    @Override
    public IPage<LanyaDevice> listPage(PageForm pageForm,ApDeviceForm apDeviceForm) {


        return baseMapper.selectDevicePage(new Page<>(pageForm.getCurrent(),pageForm.getSize()),apDeviceForm);
    }

    @Override
    public List<SpoInfo> getOnlineSpo() {
        List<SpoInfo> records = new ArrayList<>();
        Set<String> spos = ConDeviceManage.getInstance().getConnectedSpo();
        for (String spo : spos
                ) {
            DataCache dataCache = SpoPolicy.getInstance().getDataCache(spo);
            if(DeviceType.SPO_DEVICE.equals(dataCache.getDeviceType())){
                if(dataCache.getSpoInfo()==null){
                    dataCache.getSpoInfo().setDeviceId(spo);
                }
                records.add(dataCache.getSpoInfo());
            }

        }
        return records;
    }

    @Override
    public List<BpInfo> getOnlineBp() {
        List<BpInfo> records = new ArrayList<>();
        Set<String> spos = ConDeviceManage.getInstance().getConnectedSpo();
        for (String bp : spos
                ) {
            DataCache dataCache = SpoPolicy.getInstance().getDataCache(bp);
            if(DeviceType.BP_DEVICE.equals(dataCache.getDeviceType())){
                if(dataCache.getBpInfo()==null){
                    dataCache.getBpInfo().setDeviceId(bp);
                }
                records.add(dataCache.getBpInfo());
            }
        }
        return records;
    }

}
